package com.jingwei.activiti;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 经纬
 * @since 2025/2/11 15:50
 */
@SpringBootTest
public class activitiDemo {


    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;


    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {

        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3、使用service进行流程的部署，部署bpmn和png
        Deployment deploy = repositoryService.createDeployment()
                .name("维修工单流程")//流程名称
                .addClasspathResource("bpmn/repairOrder.bpmn20.xml")//对应的bpmn文件
//                .addClasspathResource("bpmn/cc.bpmn20.png")
                .deploy();
        System.out.println(deploy);
    }

    /**
     * 删除流程部署
     */
    @Test
    public void deleteDeploy() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3、删除部署的流程定义
        repositoryService.deleteDeployment("51257db3-e8e9-11ef-890c-aab857732eaf", true);
    }


    /**
     * 查询部署流程列表
     */
    @Test
    public void getDeployList() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RepositoryServcie(re表的服务层)\
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //3、查询部署的流程定义
        List<Deployment> list = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : list) {
            System.out.println("流程id=" + deployment.getId());
            System.out.println("流程名称=" + deployment.getName());
        }
    }

    /**
     * 实例化流程
     */
    @Test
    public void startProcess() {
        //流程bpmn的名称
        String processDefinitionKey = "repairOrder";
        //业务
        String businessKey = "1001";
        //流程变量
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("repairOrderId", "123456");
        variables.put("repairDevice", "空调");
        variables.put("repairYy", "空调漏水了快修");
        variables.put("repairType", "大修");
        variables.put("start", "小明");
        variables.put("fqr", "小明发起人");
        variables.put("qyfzr", "区域负责人");


        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取RuntimeService(ru表的服务层)\
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //3、启动流程实例，key对应bpmn文件中的id属性值
        String processInstanceId = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variables).getId();
        System.out.println("流程实例ID=" + processInstanceId);
    }


    /**
     * 审核流程任务
     */
    @Test
    public void taskFlow() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("repairOrderId", "123456");
        variables.put("repairDevice", "空调");
        variables.put("repairYy", "空调漏水了快修");
        variables.put("repairType", "大修");
        variables.put("start", "小明");
        variables.put("qyfzr", "区域负责人");
        variables.put("weixiuzhe", "维修人员1");
        variables.put("wxrqr", "维修人员1");
        variables.put("fqr", "维修人员1");
        variables.put("repairAffirm", true);
        variables.put("audit", true);
        variables.put("gdwx", "工单维修");
        variables.put("repairCheck", true);
        variables.put("检查", "工单维修");
        taskService.setVariables("4e17977f-e902-11ef-8cc4-fa48906c19ec", variables);
        taskService.complete("4e17977f-e902-11ef-8cc4-fa48906c19ec", variables);
        Map<String, Object> variables1 = taskService.getVariables("4e17977f-e902-11ef-8cc4-fa48906c19ec");
        System.out.println(variables1.toString());
    }

    @Test
    public void findid() {
        //通过taskid查询流程实例
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskId("72d9617b-e902-11ef-b03d-fa48906c19ec");
        Task task = taskQuery.singleResult();
        System.out.println(task.getProcessVariables().toString());
        System.out.println(task.getProcessVariables().toString());
    }


    /**
     * 查询代办任务
     */
    @Test
    public void findTodoTask() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取TaskService(ta表的服务层)\
        TaskService taskService = processEngine.getTaskService();
        //3、查询待办任务
        List<org.activiti.engine.task.Task> list = taskService.createTaskQuery().list();
        for (org.activiti.engine.task.Task task : list) {
            System.out.println("任务ID=" + task.getId());
            System.out.println("任务名称=" + task.getName());
            System.out.println("任务的办理人=" + task.getAssignee());
        }
    }

    /**
     * 查询任务详情
     */
    @Test
    public void findTaskDetail() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取TaskService(ta表的服务层)\
        TaskService taskService = processEngine.getTaskService();
        //3、查询待办任务
        org.activiti.engine.task.Task task = taskService.createTaskQuery().taskId("67a7f857-e8e9-11ef-a5f4-aab857732eaf").singleResult();
        System.out.println("任务ID=" + task.getId());
        System.out.println("任务名称=" + task.getName());
        System.out.println("任务的办理人=" + task.getAssignee());
        Map<String, Object> processVariables = task.getProcessVariables();
        System.out.println("流程变量=" + processVariables);
    }


    /**
     * 查询流程
     */
    @Test
    public void findTiskByName() {
        List<Task> list = taskService.createTaskQuery().taskAssignee("小明报修者").list();
        System.out.println(list.toString());
        list.forEach(task -> {
            System.out.println("任务ID=" + task.getId());
            System.out.println("任务名称=" + task.getName());
            System.out.println("任务的办理人=" + task.getAssignee());
            Map<String, Object> processVariables = task.getProcessVariables();
            System.out.println("流程变量=" + processVariables);
        });
        System.out.println(list);
    }

    /**
     * 查询任务
     */
    @Test
    public void findByNameTask() {
        //1、创建（流程引擎）ProcessEngine
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //2、获取TaskService(ta表的服务层)\
        TaskService taskService = processEngine.getTaskService();
        //3、查询待办任务
//        List<org.activiti.engine.task.Task> list = taskService.createTaskQuery().taskName("区域负责人审核").list();
        List<org.activiti.engine.task.Task> list = taskService.createTaskQuery().taskId("92463a03-e853-11ef-bd38-366b80e6adfc").list();
        for (org.activiti.engine.task.Task task : list) {
            System.out.println("任务ID=" + task.getId());
            System.out.println("任务名称=" + task.getName());
            System.out.println("任务的办理人=" + task.getAssignee());
        }
    }


}
